package cn.itsource.pethome.system.user.controller;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.logininfo.domain.Logininfo;
import cn.itsource.pethome.system.logininfo.service.ILogininfoService;
import cn.itsource.pethome.system.user.service.IUserService;
import cn.itsource.pethome.system.user.domain.User;
import cn.itsource.pethome.system.user.query.UserQuery;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户API",description="用户相关的CRUD功能")
public class UserController {
    @Resource
    public IUserService userService;
    @Resource
    private ILogininfoService logininfoService;

    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null){
                userService.update(user);
                return AjaxResult.success().setMsg("修改成功!");
            }
            else{
                userService.add(user);
                return AjaxResult.success().setMsg("添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public User get(@PathVariable("id")Long id)
    {
        return userService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<User> list(){
        return userService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody UserQuery query)
    {
        try {
            List<User> query1 = userService.findQuery(query);
            Long aLong = userService.countPage(query);
            return AjaxResult.success().setData(query1).setTotal(aLong);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @GetMapping("/activation/{id}")
    public String userActivation(@PathVariable Long id){
        User user = userService.findOne(id);
        user.setState(BaseConstants.User.STATE_NORMAL);
        userService.update(user);
        //同步logininfo表
        Logininfo logininfo = logininfoService.findOne(user.getLogininfoId());
        logininfo.setDisable(BaseConstants.Login.DISABLE_1);
        logininfoService.update(logininfo);
        return "恭喜您，激活成功！";
    }
}
